const moment = require('moment');

module.exports = function ({GoodsesDetails, sequelize}) {

    /**
     * 保存数据
     */
    GoodsesDetails.saveV2 = function (goods_id, body) {
        let date = moment().format('YYYYMMDDHH');
        return GoodsesDetails.findOne({
            where: {goods_id: goods_id, date: date},
            attributes: ['id']
        }).then(function (goods) {
            if (goods) {
                return GoodsesDetails.update(body, {where: {id: goods.id}});
            }
            body.goods_id = goods_id;
            body.date = date;
            return GoodsesDetails.create(body);
        });
    }

    GoodsesDetails.couponsV2 = function (goods_id) {
        let date = moment().format('YYYYMMDDHH');
        return GoodsesDetails.findOne({
            where: {goods_id: goods_id, date: date},
            attributes: ['id', 'coupons_data']
        }).then(function (goods) {
            if (goods && goods.coupons_data) {
                return JSON.parse(goods.coupons_data);
            }
        });
    }

}